package com.mikelouis.test.scala.proginscala.chapter3

import java.math.BigDecimal

/**
 * Created by yuazhu on 12/18/2014.
 */
object list {
  def main(args: Array[String]) {
    val big = new BigDecimal("12345")
    println(big)

    /** Arrary test */
    val greetStrings: Array[String] = new Array[String](3)
    greetStrings(0) = "Hello"
    greetStrings(1) = ", "
    greetStrings(2) = "world!\n"
    // for (i <- 0 to 2) print(greetStrings(i))
    for (i <- 0.to(2)) print(greetStrings(i))

    // equivalent code with the code above
    val greetStrings2 = new Array[String](3)
    greetStrings2.update(0, "Hello")
    greetStrings2.update(1, ", ")
    greetStrings2.update(2, "world!\n")
    for (i <- 0.to(2)) print(greetStrings2.apply(i))

    println("******************************")
    // 编译器推断数组的类型是Array[String] ，因为你把字串传给它
    val numName = Array("zero", "one", "two")
    for (num <- numName) print(num.+(","))

    println()
    // equivalent code
    val numName2 = Array.apply("zero", "one", "two")
    numName2.foreach(num => print(num + ", "))
    println()

    /** List test */
    println("***************List***************")
    //    val oneTwoThree = List(1, 2, 3)
    val oneTwo = List(1, 2)
    val threeFour = List(3, 4)
    // 叠加两个列表（返回带1, 2, 3, 4的新List[Int]）
    val oneTwoThreeFour = oneTwo ::: threeFour
    println(oneTwo + " and " + threeFour + " were not mutated.")
    println("Thus, " + oneTwoThreeFour + " is a new List.")
    // :: Cons把一个新元素组合到已有List的最前端，然后返回结果List
    val oneOneTwo = 1 :: oneTwo
    println(oneOneTwo)
    // 表达式“1 :: twoThree”中，::是它右操作数，列表twoThree，的方法
    println(oneTwo.::(1))

    // create a list
    // 要在最后用到Nil的理由是::是定义在List类上的方法。如果你想只是写成1 :: 2 :: 3，由于3是Int类型，没有::方法，因此会导致编译失败
    val oneTwoThree = 1::2::3::Nil
    println(oneTwoThree)
  }
}
